// pages/launch/launch.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    page: 1,
    thumbUrl: getApp().globalData.thumbUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var uid = wx.getStorageSync('uid');
    that.updateData(1, 1, 1);
  },
  countDownCallbackFn: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    var show = that.data.show;
    var param = {
      id: id
    };
    app.wxRequest('POST', "Index/checkGroupStatus", param, (res) => {
      var code = res.code;
      if (code == 1) {
        setTimeout(function () {
          if (show == true) {
            that.onLoad();
          }
        }, 1000)
      }
    }, (err) => {
      console.log(err.errMsg)
    })
    console.log(e)
  },
  updateData: function (type, status, page) {
    var that = this;
    var uid = wx.getStorageSync('uid');
    var param = {
      uid: uid,
      type: type,
      status: status,
      page: page
    };
    app.wxRequest('POST', "Index/ptList", param, (res) => {
      var data = res.data;
      that.setData({
        list: data
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  showTab: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    if (type == 1) {
      that.setData({
        show: true,
        page: 1
      })
      that.updateData(1, 1, 1);
    } else if (type == 2) {
      that.setData({
        show: false,
        page: 1
      })
      that.updateData(1, 2, 1);
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onUnload: function () {
    var that = this;
    that.setData({
      clearTimer: true
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    var show = that.data.show;
    if (show == true) {
      var status = 1;
    } else {
      var status = 2;
    }
    wx.showNavigationBarLoading();
    that.updateData(1, status, 1);
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 3000)

    // that.
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var show = that.data.show;
    var page = that.data.page;
    var uid = wx.getStorageSync('uid');
    var newPage = parseInt(page) + 1;
    var list = that.data.list;
    if (show == true) {
      var status = 1;
    } else {
      var status = 2;
    }
    var param = {
      type: 1,
      uid: uid,
      status: status,
      page: newPage
    }
    app.wxRequest('POST', "Index/ptList", param, (res) => {
      var data = res.data;
      var code = res.code;
      if (code == 1) {
        that.setData({
          list: list.concat(data),
          page: newPage
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1500
        })
      }

    }, (err) => {
      console.log(err.errMsg)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    console.log(e)
    var that = this;
    var id = e.target.dataset.id;
    var tid = e.target.dataset.tid;
    var list = that.data.list;

    for (var i in list) {
      if (list[i].goodsInfo.id == tid) {
        var goodsName = list[i].goodsInfo.name;
        var picture = list[i].goodsInfo.picture;
        break;
      }
    }
    var thumbUrl = that.data.thumbUrl;
    var uid = wx.getStorageSync('uid')
    return {
      title: goodsName,
      imageUrl: thumbUrl + picture,
      path: '/pages/shopDetails/shopDetails?id=' + tid + "&name=" + goodsName + "&uid=" + uid
    }
  }
})